﻿using DTcms.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace DTcms.Web.Mvc.UI
{
    public partial class BaseController : Controller
    {
        #region 获取上一条下一条的链接
        /// <summary>
        /// 获取上一条下一条的链接
        /// </summary>
        /// <param name="urlkey">urlkey</param>
        /// <param name="type">-1代表上一条，1代表下一条</param>
        /// <param name="defaultvalue">默认文本</param>
        /// <param name="callIndex">是否使用别名，0使用ID，1使用别名</param>
        /// <param name="category_id">类别ID</param>
        /// <param name="channel_id">频道ID</param>
        /// <param name="channel">频道名</param>
        /// <param name="id">文章ID</param>
        /// <returns>A链接</returns>
        public string get_prevandnext_article(string urlkey, int type, string defaultvalue, int callIndex, int category_id, string channel, int id)
        {
            string symbol = (type == -1 ? "<" : ">");
            BLL.article bll = new BLL.article();
            string str = string.Empty;
            str = "category_id=" + category_id;

            string orderby = type == -1 ? "id desc" : "id asc";
            DataSet ds = bll.ArticleList(channel, 1, str + " and status=0 and Id" + symbol + id, orderby);
            if (ds == null || ds.Tables[0].Rows.Count <= 0)
            {
                return string.Format("<span>{0}</span>", defaultvalue);
            }
            if (callIndex == 1 && !string.IsNullOrEmpty(ds.Tables[0].Rows[0]["call_index"].ToString()))
            {
                return "<a href=\"" + linkurl(urlkey, ds.Tables[0].Rows[0]["call_index"].ToString()) + "\">" + ds.Tables[0].Rows[0]["title"] + "</a>";
            }
            return "<a href=\"" + linkurl(urlkey, ds.Tables[0].Rows[0]["id"].ToString()) + "\">" + ds.Tables[0].Rows[0]["title"] + "</a>";
        }
        #endregion

        #region 列表标签
        /// <summary>
        /// 随机文章列表
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        //public DataTable get_article_random_list(string channel_name, int category_id, int top, string strwhere)
        //{
        //    DataTable dt = new DataTable();
        //    if (!string.IsNullOrEmpty(channel_name))
        //    {
        //        dt = new BLL.article().GetRandomList(channel_name, category_id, top, strwhere).Tables[0];
        //    }
        //    return dt;
        //}

        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        public DataTable get_article_list(string channel_name, int top, string strwhere)
        {
            DataTable dt = new DataTable();
            if (!string.IsNullOrEmpty(channel_name))
            {
                dt = new BLL.article().ArticleList(channel_name, top, strwhere, "sort_id asc,add_time desc").Tables[0];
            }
            return dt;
        }

        /// <summary>
        /// 文章专题列表
        /// </summary>
        /// <param name="channel_name">频道名</param>
        /// <param name="ztid">专题主ID</param>
        /// <param name="classid">专题分类ID</param>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="filedOrder">排序条件</param>
        /// <returns>DataTable</returns>
        public DataTable get_zhuanti_article_list(string channel_name, int ztid, int classid, int top, string strwhere, string filedOrder)
        {
            DataTable dt = new DataTable();
            if (!string.IsNullOrEmpty(channel_name))
            {
                dt = new BLL.article().ArticleZhuantiList(channel_name, ztid, classid, top, strwhere, filedOrder).Tables[0];
            }
            return dt;
        }

        /// <summary>
        /// 获取同类文章
        /// </summary>
        /// <param name="channel_name">频道名</param>
        /// <param name="tonglei">同类值</param>
        /// <param name="top">显示条数</param>
        /// <returns></returns>
        public List<Model.article> get_tonglei_article_list(string channel_name, string tonglei, int top)
        {
            List<Model.article> ls = new List<Model.article>();
            string[] res = tonglei.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < res.Length; i++)
            {
                if (i <= top)
                {
                    Model.article model = new BLL.article().GetModel(channel_name, int.Parse(res[i]));
                    if (model != null)
                    {
                        ls.Add(model);
                    }
                }
            }
            return ls;
        }

        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">分类ID</param>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns>DataTable</returns>
        public DataTable get_article_list(string channel_name, int category_id, int top, string strwhere)
        {
            DataTable dt = new DataTable();
            if (!string.IsNullOrEmpty(channel_name))
            {
                dt = new BLL.article().ArticleList(channel_name, category_id, top, strwhere, "sort_id asc,add_time desc").Tables[0];
            }
            return dt;
        }

        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">分类ID</param>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="orderby">排序</param>
        /// <returns>DataTable</returns>
        public DataTable get_article_list(string channel_name, int category_id, int top, string strwhere, string orderby)
        {
            DataTable dt = new DataTable();
            if (!string.IsNullOrEmpty(channel_name))
            {
                dt = new BLL.article().ArticleList(channel_name, category_id, top, strwhere, orderby).Tables[0];
            }
            return dt;
        }

        /// <summary>
        /// 文章分页列表(自定义页面大小)
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">分类ID</param>
        /// <param name="page_size">页面大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="orderby">排序</param>
        /// <param name="totalcount">总记录数</param>
        /// <returns>DateTable</returns>
        public DataTable get_article_list(string channel_name, int category_id, int page_size, int page_index, string strwhere, string orderby, out int totalcount)
        {
            DataTable dt = new DataTable();
            if (!string.IsNullOrEmpty(channel_name))
            {
                dt = new BLL.article().ArticleList(channel_name, category_id, page_size, page_index, strwhere, orderby, out totalcount).Tables[0];
            }
            else
            {
                totalcount = 0;
            }
            return dt;
        }

        /// <summary>
        /// 文章分页列表(自动页面大小)
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">分类ID</param>
        /// <param name="page_size">分页大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalcount">总记录数</param>
        /// <param name="_key">URL配置名称</param>
        /// <param name="_params">传输参数</param>
        /// <returns>DataTable</returns>
        public DataTable get_article_list(string channel_name, int category_id, int page_size, int page_index, string strwhere, out int totalcount, out string pagelist, string _key, params object[] _params)
        {
            DataTable dt = new DataTable();
            if (!string.IsNullOrEmpty(channel_name))
            {
                dt = new BLL.article().ArticleList(channel_name, category_id, page_size, page_index, strwhere, "sort_id asc,add_time desc", out totalcount).Tables[0];
                pagelist = Utils.OutPageList(page_size, page_index, totalcount, linkurl(_key, _params), 8);
            }
            else
            {
                totalcount = 0;
                pagelist = "";
            }
            return dt;
        }

        /// <summary>
        /// 文章分页列表(自动页面大小)
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">分类ID</param>
        /// <param name="page_size">分页大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalcount">总记录数</param>
        /// <param name="_key">URL配置名称</param>
        /// <param name="_call_index">调用名</param>
        /// <param name="_id">类别ID</param>
        /// <returns>DataTable</returns>
        public DataTable get_article_list_pag(string channel_name, int category_id, int page_size, int page_index, string strwhere, out int totalcount, out string pagelist, string _key, string _call_index, int _id)
        {
            if (!string.IsNullOrEmpty(_call_index))
            {
                return get_article_list(channel_name, category_id, page_size, page_index, strwhere, out totalcount, out pagelist, _key, _call_index, "__id__");
            }
            else
            {
                return get_article_list(channel_name, category_id, page_size, page_index, strwhere, out totalcount, out pagelist, _key, _id, "__id__");
            }
        }

        /// <summary>
        /// 文章分页列表(可排序)
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">分类ID</param>
        /// <param name="page_size">分页大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="orderby">排序</param>
        /// <param name="totalcount">总记录数</param>
        /// <param name="_key">URL配置名称</param>
        /// <param name="_params">传输参数</param>
        /// <returns>DataTable</returns>
        public DataTable get_article_list(string channel_name, int category_id, int page_size, int page_index, string strwhere, string orderby, out int totalcount, out string pagelist, string _key, params object[] _params)
        {
            DataTable dt = new DataTable();
            if (!string.IsNullOrEmpty(channel_name))
            {
                dt = new BLL.article().ArticleList(channel_name, category_id, page_size, page_index, strwhere, orderby, out totalcount).Tables[0];
                pagelist = Utils.OutPageList(page_size, page_index, totalcount, linkurl(_key, _params), 8);
            }
            else
            {
                totalcount = 0;
                pagelist = "";
            }
            return dt;
        }

        /// <summary>
        /// 获取站点的所有文章  根据关健字查询分页数据
        /// </summary>
        /// <param name="site_id">站点ID</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="strWhere">条件过滤</param>
        /// <param name="filedOrder">排序</param>
        /// <param name="recordCount">文章总数</param>
        /// <returns></returns>
        public DataSet get_article_all(int site_id, int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            DataSet dt = new DataSet();
            if (site_id == 0)
            {
                recordCount = 0;
            }
            else
            {
                dt = new BLL.article().ArticleSearch(site_id, pageSize, pageIndex, strWhere, filedOrder, out recordCount);
            }
            return dt;
        }
        #endregion

        #region 内容标签
        /// <summary>
        /// 根据ID取得内容图片列表
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>String</returns>
        public DataTable get_article_albums(int article_id)
        {
            if (article_id > 0)
            {
                return new BLL.article_albums().GetImagesList(article_id);
            }
            return null;
        }

        //图片列表
        public List<Model.article_albums> get_article_albums(int channel_id, int article_id)
        {
            if (article_id > 0 && channel_id > 0)
            {
                return new BLL.article_albums().GetList(channel_id, article_id);
            }
            return null;
        }

        //附件列表
        public List<Model.article_attach> get_article_attach(int channel_id, int article_id)
        {
            if (article_id > 0 && channel_id > 0)
            {
                return new BLL.article_attach().GetList(channel_id, article_id);
            }
            return null;
        }

        //附件详情
        public Model.article_attach get_article_attach_info(int attach_id)
        {
            if (attach_id > 0)
            {
                return new BLL.article_attach().GetModel(attach_id);
            }
            return new Model.article_attach();
        }
        /// <summary>
        /// 附件下载列表
        /// </summary>
        /// <param name="page_size">页面大小</param>
        /// <param name="page_index">当前页码</param>
        /// <param name="strwhere">查询条件</param>
        /// <param name="totalcount">总记录数</param>
        /// <returns>DateTable</returns>
        public List<Model.user_attach_log> get_article_attach_log(int page_size, int page_index, string strwhere, out int totalcount)
        {
            var resultlist = new BLL.user_attach_log().GetQueryPage(page_index, page_size, strwhere, "add_time desc,id desc");
            totalcount = Utils.ObjToInt(resultlist.TotalItems);
            return resultlist.Items;
        }

        /// <summary>
        /// 检查用户是否下载过该附件
        /// </summary>
        public bool ExistsLog(int attach_id, int user_id)
        {
            BLL.article_attach bll = new BLL.article_attach();
            return bll.ExistsLog(attach_id, user_id);
        }

        /// <summary>
        /// 附件下载列表
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="strwhere">查询条件</param>
        /// <returns></returns>
        public List<Model.user_attach_log> get_article_attach_log(int top, string strwhere)
        {
            return new BLL.user_attach_log().GetModelList(top, strwhere, "*", "add_time desc,id desc");
        }

        //自定义参数列表
        public List<Model.article_attribute> get_article_attribute(int channel_id, int article_id)
        {
            if (article_id > 0 && channel_id > 0)
            {
                return new BLL.article_attribute().GetList(channel_id, article_id);
            }
            return null;
        }

        /// <summary>
        /// 根据ID取得模型
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>String</returns>
        public Model.article get_article_model(int channel_id, int id)
        {
            if (id > 0)
            {
                BLL.article bll = new BLL.article();
                if (bll.Exists(channel_id, id))
                {
                    return bll.GetModel(channel_id, id);
                }
            }
            return null;
        }

        /// <summary>
        /// 根据channel_name取得模型
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>String</returns>
        public Model.article get_article_model(string channel_name, int id)
        {
            if (id > 0)
            {
                BLL.article bll = new BLL.article();
                if (bll.ArticleExists(channel_name, id))
                {
                    return bll.ArticleModel(channel_name, id);
                }
            }
            return null;
        }

        /// <summary>
        /// 根据调用标识取得内容
        /// </summary>
        /// <param name="call_index">调用别名</param>
        /// <returns>String</returns>
        public string get_article_content(int channel_id, string call_index)
        {
            if (string.IsNullOrEmpty(call_index))
                return string.Empty;
            BLL.article bll = new BLL.article();
            if (bll.Exists(channel_id, call_index))
            {
                return bll.GetModel(channel_id, call_index).content;
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取对应的图片路径
        /// </summary>
        /// <param name="article_id">信息ID</param>
        /// <returns>String</returns>
        public string get_article_img_url(int channel_id, int article_id)
        {
            Model.article model = new BLL.article().GetModel(channel_id, article_id);
            if (model != null)
            {
                return model.img_url;
            }
            return "";
        }

        /// 获取对应的标题
        /// </summary>
        /// <param name="article_id">信息ID</param>
        /// <returns>String</returns>
        public string get_article_title(int channel_id, int article_id)
        {
            string title = new BLL.article().GetTitle(channel_id, article_id);
            if (string.IsNullOrEmpty(title))
            {
                return "";
            }
            return title;
        }

        /// <summary>
        /// 获取对应的图片路径
        /// </summary>
        /// <param name="article_id">信息ID</param>
        /// <returns>String</returns>
        public string get_article_img_url(string channel_name, int article_id)
        {
            Model.article model = new BLL.article().GetModel(channel_name, article_id);
            if (model != null)
            {
                return model.img_url;
            }
            return "";
        }

        /// <summary>
        /// 获取对应的标题
        /// </summary>
        /// <param name="article_id">信息ID</param>
        /// <returns>String</returns>
        public string get_article_title(string channel_name, int article_id)
        {
            Model.article model = new BLL.article().GetModel(channel_name, article_id);
            if (model != null)
            {
                return model.title;
            }
            return "";
        }

        /// <summary>
        /// 获取对应的频道名称
        /// </summary>
        /// <param name="channel_id">频道ID</param>
        /// <returns>String</returns>
        public string get_channel_name(int channel_id)
        {
            string name = new BLL.site_channel().GetChannelName(channel_id);
            if (!string.IsNullOrEmpty(name))
            {
                return name;
            }
            return "";
        }

        /// <summary>
        /// 获取对应的频道标题
        /// </summary>
        /// <param name="channel_id">频道ID</param>
        /// <returns>String</returns>
        public string get_channel_title(int channel_id)
        {
            string name = new BLL.site_channel().GetChannelTitle(channel_id);
            if (!string.IsNullOrEmpty(name))
            {
                return name;
            }
            return "";
        }

        /// <summary>
        /// 获取扩展字段的值
        /// </summary>
        /// <param name="article_id">内容ID</param>
        /// <param name="field_name">扩展字段名</param>
        /// <returns>String</returns>
        public string get_article_field(int channel_id, int article_id, string field_name)
        {
            Model.article model = new BLL.article().GetModel(channel_id, article_id);
            if (model != null && model.fields.ContainsKey(field_name))
            {
                return model.fields[field_name];
            }
            return string.Empty;
        }
        public string get_article_field(string channel_name, int article_id, string field_name)
        {
            Model.article model = new BLL.article().GetModel(channel_name, article_id);
            if (model != null && model.fields.ContainsKey(field_name))
            {
                return model.fields[field_name];
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取扩展字段的值
        /// </summary>
        /// <param name="call_index">调用别名</param>
        /// <param name="field_name">扩展字段名</param>
        /// <returns>String</returns>
        public string get_article_field(int channel_id, string call_index, string field_name)
        {
            if (string.IsNullOrEmpty(call_index))
                return string.Empty;
            BLL.article bll = new BLL.article();
            if (!bll.Exists(channel_id, call_index))
            {
                return string.Empty;
            }
            Model.article model = bll.GetModel(channel_id, call_index);
            if (model != null && model.fields.ContainsKey(field_name))
            {
                return model.fields[field_name];
            }
            return string.Empty;
        }
        #endregion

        #region  自增方法
        /// <summary>
        /// 是否为视频文件
        /// </summary>
        /// <param name="_fileExt">文件扩展名，不含“.”</param>
        public bool IsVideo(string _fileExt)
        {
            ArrayList al = new ArrayList();
            al.Add(".mp3");
            al.Add(".mp4");
            al.Add(".avi");
            al.Add(".flv");
            if (al.Contains(_fileExt.ToLower()))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 截取字符串长度标签
        /// </summary>
        /// <param name="str"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public string cutstring(object str, int num)
        {
            return Utils.DropHTML(str.ToString(), num);
        }

        //将日期转为指定格式
        public string datetostr(object time, string str)
        {
            if (time == null)
            {
                time = DateTime.Now;
            }
            return Utils.ObjectToDateTime(time).ToString(str);
        }

        //计算两个日期之间相差的天数
        public int DateDiff(DateTime dateStart, DateTime dateEnd)
        {
            DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
            DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());

            TimeSpan sp = end.Subtract(start);

            return sp.Days;
        }

        //统计数量
        public int ArticleCount()
        {
            var list = new BLL.site_channel().GetList();
            int count = 0;
            foreach (var item in list)
            {
                count += new BLL.article().GetCount(item.name, "status=0");
            }
            return count;
        }

        //文章数量
        public int ArticleCount(string channel_name, int category_id, string strWhere)
        {
            return new BLL.article().ArticleCount(channel_name, category_id, strWhere);
        }

        //文章相册图片数量
        public int AlbumsCount(int channel_id, int article_id)
        {
            if (channel_id == 0 || article_id == 0)
            {
                return 0;
            }
            string strWhere = string.Format("channel_id={0} and article_id={1}", channel_id, article_id);
            int rows = Utils.StrToInt(new BLL.article_albums().GetCount("count(1)", strWhere));
            return rows;
        }

        //下载数量
        public int AttachCount(int attach_id)
        {
            BLL.user_attach_log bll = new BLL.user_attach_log();
            string strwhere = "";
            if (attach_id > 0)
            {
                strwhere = "attach_id=" + attach_id;
            }
            int rows = Utils.StrToInt(bll.GetCount("count(1)", strwhere));
            return rows;
        }

        //下载数量
        public int AttachCount(int articleid, int channle)
        {
            BLL.article_attach bll = new BLL.article_attach();
            if (articleid == 0 || channle == 0)
            {
                return 0;
            }
            List<Model.article_attach> list = bll.GetModelList(0, string.Format("channel_id={0} and article_id={1}", channle, articleid), "id", "");
            if (list.Count == 0)
            {
                return 0;
            }
            string[] ids = list.Select(t => t.id.ToString()).ToArray();
            string rows = new BLL.user_attach_log().GetCount("count(1)", "attach_id in(" + string.Join(",", ids) + ")");
            return Utils.StrToInt(rows);
        }

        //判断是否为null，并返回当前Model扩展字段值
        public string ArticleField(Dictionary<string, string> m, string field)
        {
            string str = string.Empty;
            try
            {
                str = m[field];
            }
            catch
            {

            }
            return str;
        }

        //判断select是否是同一个值 并返回selected
        public string ArticleSelect(object filed, object value)
        {
            return filed.ToString() == value.ToString() ? "selected" : "";
        }

        //判断checkbox是否是同一个值 并返回checked
        public string ArticleCheckbox(object filed, object value)
        {
            return filed.ToString() == value.ToString() ? "checked" : "";
        }

        //判断值是否存在 并返回状态 0、1
        public int ArticleDisplay(object value1, object value2)
        {
            return value1.ToString().Contains(value2.ToString()) ? 1 : 0;
        }

        //获取商家自定义扩展参数的Json
        public string get_article_attribute(string _strWhere, string _orderby)
        {
            BLL.article_attribute bll = new BLL.article_attribute();
            List<Model.article_attribute> role_list = bll.GetModelList(0, _strWhere, "", _orderby);
            List<Model.AttributeFild> attribute = new List<Model.AttributeFild>();
            foreach (var item in role_list)
            {
                attribute.Add(new Model.AttributeFild { id = 0, chrkey = item.title + ":" + item.value });
            }
            return JsonHelper.ObjectToJSON(new { form_type2 = attribute });
        }

        //根据文章内容获得其中的图片
        public List<string> ContentImg(List<string> Img)
        {
            List<string> img = new List<string>();

            // 定义正则表达式用来匹配 img 标签 
            Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
            foreach (string item in Img)
            {
                // 搜索匹配的字符串 
                MatchCollection matches = regImg.Matches(item);

                // 取得匹配项列表 
                foreach (Match match in matches)
                {
                    img.Add(match.Groups["imgUrl"].Value);
                }
            }
            return img;
        }

        public string ContentImg(string chrcontent)
        {
            string img = string.Empty;
            // 定义正则表达式用来匹配 img 标签 
            Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
            // 搜索匹配的字符串 
            MatchCollection matches = regImg.Matches(chrcontent);
            // 取得匹配项列表 
            foreach (Match match in matches)
            {
                img = match.Groups["imgUrl"].Value;
            }
            return img;
        }
        #endregion

        #region 搜索
        /// <summary>
        /// 获取URL参数
        /// </summary>
        /// <param name="key">参数名称</param>
        /// <returns>参数值</returns>
        public int GetInt(object key)
        {
            if (key == null)
            {
                return 0;
            }
            return DTRequest.GetQueryInt(key.ToString());
        }

        /// <summary>
        /// 获取URL参数
        /// </summary>
        /// <param name="key">参数名称</param>
        /// <returns>参数值</returns>
        public string GetString(object key)
        {
            if (key == null)
            {
                return string.Empty;
            }
            return DTRequest.GetQueryString(key.ToString());
        }

        /// <summary>
        /// 获取URL参数
        /// </summary>
        /// <param name="key">参数名称</param>
        /// <returns>参数值</returns>
        public string GetString(object key, string value)
        {
            if (key == null)
            {
                return value;
            }
            return DTRequest.GetQueryStringValue(key.ToString(), value);
        }

        /// <summary>
        /// 获取URL
        /// </summary>
        /// <returns></returns>
        public string GetUrl(Dictionary<string, string> parameter)
        {
            StringBuilder url = new StringBuilder();
            foreach (var t in parameter)
            {
                url.AppendFormat("{0}={1}&", t.Key, t.Value);
            }
            return url.ToString().TrimEnd('&');
        }

        /// <summary>
        /// 获取URL(删除指定参数)
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public string GetUrl(Dictionary<string, string> parameter, string key)
        {
            StringBuilder url = new StringBuilder();
            foreach (var t in parameter)
            {
                if (t.Key != key)
                {
                    url.AppendFormat("{0}={1}&", t.Key, t.Value);
                }
            }
            return url.ToString().TrimEnd('&');
        }

        /// <summary>
        /// 获取URL（添加指定参数）
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public string GetUrl(Dictionary<string, string> parameter, string key, string value)
        {
            bool isContain = false;
            StringBuilder url = new StringBuilder();
            foreach (var t in parameter)
            {
                if (t.Key == key)
                {
                    isContain = true;
                    url.AppendFormat("{0}={1}&", key, value);
                }
                else
                {
                    url.AppendFormat("{0}={1}&", t.Key, t.Value);
                }
            }
            if (!isContain && !string.IsNullOrEmpty(value))
            {
                url.AppendFormat("{0}={1}&", key, value);
            }
            return url.ToString().TrimEnd('&');
        }

        /// <summary>
        /// 获取URL(删除指定参数)
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public string GetEvenUrl(Dictionary<string, string> parameter, string key)
        {
            string[] k = key.Split(',');
            StringBuilder url = new StringBuilder();
            foreach (var t in parameter)
            {
                if (!k.Contains(t.Key))
                {
                    url.AppendFormat("{0}={1}&", t.Key, t.Value);
                }
            }
            return url.ToString().TrimEnd('&');
        }

        /// <summary>
        /// 获取URL（批量添加指定参数）
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public string GetEvenUrl(Dictionary<string, string> parameter, string key, string value)
        {
            string[] k = key.Split(',');
            string[] v = value.Split(',');
            StringBuilder url = new StringBuilder();
            foreach (var t in parameter)
            {
                if (!key.Contains(t.Key))
                {
                    url.AppendFormat("{0}={1}&", t.Key, t.Value);
                }
            }
            for (int i = 0; i < k.Length; i++)
            {
                url.AppendFormat("{0}={1}&", k[i], v[i]);
            }
            return url.ToString().TrimEnd('&');
        }
        #endregion
    }
}